package Global;

import jplay.Window;

public class FPSWindow extends Window {
    private long fps;
    private long delta; // nanoseconds
    private long tempoAnterior = System.nanoTime();
    
    public FPSWindow(int width, int height) {
        this(width, height, 30);
    }

    public FPSWindow(int width, int height, long fps) {
        super(width, height);
        this.fps = fps;
    }

    public long getFps() {
        return fps;
    }
    
    public long getFpsMax() {
        return 1000000000L / delta;
    }

    public void setFps(long fps) {
        this.fps = fps;
    }
    
    @Override
    public void update() {
        super.update();
        
        delta = System.nanoTime() - tempoAnterior;
        long delay = 1000L / fps - delta / 1000000L;
        if (delay > 0) {
            delay(delay);
        }
        tempoAnterior = System.nanoTime();        
    }
}

